using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WindowsPhonePostClient;
using System.Text.RegularExpressions;

namespace LyonMag
{
    public partial class postforum : PhoneApplicationPage
    {
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        string id = string.Empty;
        private App app = App.Current as App;
        List<Homepageclass.HomeComments> obj_comments = new List<Homepageclass.HomeComments>();
        public postforum()
        {
            InitializeComponent();
            
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (App.appSettingsGuid["accoutid"].ToString() != "test")
            {
                btnLogout.Visibility = Visibility.Visible;
            }
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            popNickName.IsOpen = false;
            popNickName.IsOpen = false;
            popLogins.IsOpen = false;
            popRigister.IsOpen = false;
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            if (txtNickName.Text != "")
            {

                postforums();


            }
            else
            {
                MessageBox.Show("Vous devez saisir un pseudo !");
            }
        }
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public void userlogin()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("login", txtUsernamePassword.Text);
            string userpassword = Base64Encode("a5"+txtPasswordPassword.Password);
            //  parameters.Add("email", "srikanth.rayavarapu5@gmail.com");
            parameters.Add("password", userpassword);

            PostClient proxy = new PostClient(parameters);
            proxy.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    //Process the result...
                    string data = e.Result;

                    //JsonConvert.PopulateObject(e.Result.ToString(), obj_userreg);



                    if (data != "ko")
                    {
                        App.appSettingsGuid["accoutid"] = data.ToString();
                        App.appSettingsGuid.Save();
                        popLogin.IsOpen = false;
                        popNickName.IsOpen = false;
                        popSubmit.IsOpen = false;
                        popLogins.IsOpen = false;
                        btnLogout.Visibility = Visibility.Visible;
                       
                        popSecondsss.IsOpen = true;
                       
                    }
                    else
                    {
                      //  MessageBox.Show(" Login ou mot de passe incorrect (si vous avez oublié votre mot de passe, rendez-vous sur notre site www.lyonmag.com)");
                        txtUserName.Text = txtNickName.Text;
                     //   popRigister.IsOpen = true;
                        popLoginFailed.IsOpen = true;
                        popNickName.IsOpen = false;

                    }

                }
            };
            proxy.DownloadStringAsync(new Uri("http://www.lyonmag.com/win8/login", UriKind.Absolute));
        }
        public void postforums()
        {
            if (App.appSettingsGuid["accoutid"].ToString() != "test")
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("name", txtNickName.Text);

                parameters.Add("account_id", App.appSettingsGuid["accoutid"].ToString());
                parameters.Add("title", txtTitle.Text);
                parameters.Add("content", txtSubject.Text);
                PostClient proxy = new PostClient(parameters);
                proxy.DownloadStringCompleted += (sender, e) =>
                {
                    if (e.Error == null)
                    {
                        //Process the result...
                        string data = e.Result;



                        //  MessageBox.Show(data);
                        popSecondsss.IsOpen = true;
                      
                        popNickName.IsOpen = false;
                        popLogin.IsOpen = false;

                        popNickName.IsOpen = false;
                        popSubmit.IsOpen = false;



                    }
                };
                proxy.DownloadStringAsync(new Uri("http://www.lyonmag.com/win8/post_forum", UriKind.Absolute));
            }
            else
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("name", txtNickName.Text);

                parameters.Add("account_id", App.appSettingsGuid["accoutid"].ToString());
                parameters.Add("title", txtTitle.Text);
                parameters.Add("content", txtSubject.Text);
                PostClient proxy = new PostClient(parameters);
                proxy.DownloadStringCompleted += (sender, e) =>
                {
                    string data = e.Result;
                    if (data == "0 Ce nom d'utilisateur est r�serv� ou incorrect. Connectez-vous s'il s'agit du votre.")
                    {
                        popLogins.IsOpen = true;
                        popNickName.IsOpen = false;
                    }
                    else
                    {
                        popSubmit.IsOpen = true;
                        popNickName.IsOpen = false;
                    }

                };
                proxy.DownloadStringAsync(new Uri("http://www.lyonmag.com/win8/post_forum", UriKind.Absolute));
            }
            
           
        }
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            popRigister.Visibility = Visibility.Visible;
            txtUserName.Text = txtNickName.Text;
            txtUsernamePassword.Text = txtNickName.Text;
            popRigister.IsOpen = true;
            popNickName.IsOpen = false;
            popSubmit.IsOpen = false;
            txtUserName.Text = txtNickName.Text;
            txtUsernamePassword.Text = txtNickName.Text;
        }
        private void btnTrynew_Click(object sender, RoutedEventArgs e)
        {
            popNickName.IsOpen = true;
            popLogins.IsOpen = false;
        }
        private void btnLoginnew_Click(object sender, RoutedEventArgs e)
        {
           
            popLogins.IsOpen = false;
            txtUsernamePassword.Text = txtNickName.Text;
            popLogin.IsOpen = true;
        }
        private void btnCamcelRegister_Click(object sender, RoutedEventArgs e)
        {
            popRigister.IsOpen = false;
            popLogin.IsOpen = false;
            popLogins.IsOpen = false;
            txtUsernamePassword.Text = txtNickName.Text;
            popNickName.IsOpen = false;
            popSubmit.IsOpen = false;
        }
        private void btnTryAgain_Click(object sender, RoutedEventArgs e)
        {
            //popLogin.IsOpen = false;
            //popNickName.IsOpen = true;
            //popSubmit.IsOpen = false;
            this.NavigationService.GoBack();
        }

        private void btnLoginCancel_Click(object sender, RoutedEventArgs e)
        {
            popLogin.IsOpen = false;
        }

        private void btnLoginUser_Click(object sender, RoutedEventArgs e)
        {
            popLogins.IsOpen = false;
            userlogin();
        }
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {

            if(txtTitle.Text.Trim()==""||txtSubject.Text.Trim()=="")
            {
                blank.IsOpen = true;
            }
            else
            {
            if (App.appSettingsGuid["accoutid"].ToString() == "test")
            {
                //popSubmit.IsOpen = true;
                popNickName.IsOpen = true;

               
               // popLogin.IsOpen = false;
            }
            else
            {
              
                postforums();
            }
            }
        }
        public void userregister()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("username", txtUserName.Text);
            string userpassword = Base64Encode(txtPasswor.Password);
            parameters.Add("email", txtEmail.Text);
            parameters.Add("password", userpassword);

            PostClient proxy = new PostClient(parameters);
            proxy.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {

                    string data = e.Result;

                    //JsonConvert.PopulateObject(e.Result.ToString(), obj_udistibuteurserreg);


                    if (data == "0 Ce nom d'utilisateur est réservé ou incorrect. Connectez-vous s'il s'agit du votre.")
                    {
                        popLogins.IsOpen = true;
                        popAlert.IsOpen = false;
                        popRigister.IsOpen = false;
                        poplogout.IsOpen = false;
                        popNickName.IsOpen = false;
                      //  MessageBox.Show(data.Remove(0,1));

                    }
                    else
                    {
                        popRigister.IsOpen = false;
                       // MessageBox.Show("Your account has been created. A confirmation email has been sent to you . For post comments with a different account, click the logout button that just appeared at the bottom of the screen");
                        popAlert.IsOpen = true;
                        App.appSettingsGuid["accoutid"] = data.ToString();
                        App.appSettingsGuid.Save();
                       
                    }

                }
            };
            proxy.DownloadStringAsync(new Uri(" http://www.lyonmag.com/win8/register", UriKind.Absolute));
        }
        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            popNickName.Visibility = Visibility.Collapsed;
            if (regex.Match(txtEmail.Text.Trim()).Success)
            {
                userregister();
               
            }
            else
            {
                popEMail.IsOpen = true;
            }
         
        }

        private void btnCancelRegister_Click(object sender, RoutedEventArgs e)
        {
            popLogin.IsOpen = false;
            popNickName.IsOpen = false;
            popRigister.IsOpen = false;
        }

        private void popafterlogout_Click(object sender, RoutedEventArgs e)
        {
            App.appSettingsGuid["accoutid"] = "test";
            App.appSettingsGuid.Save();
            poplogout.IsOpen = false;
            btnLogout.Visibility = Visibility.Collapsed;
        }

        private void btnpopupforblank_Click(object sender, RoutedEventArgs e)
        {
            blank.IsOpen = false;
        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            btnLogout.Visibility = Visibility.Collapsed;
          
                 App.appSettingsGuid["accoutid"] = "test";
            App.appSettingsGuid.Save();

            poplogout.IsOpen = true;
        }

        private void btnCloseSecond_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

        private void btnCLosein_Click(object sender, RoutedEventArgs e)
        {
            popAlert.IsOpen = false;
        }

        private void popValidEmail_Click(object sender, RoutedEventArgs e)
        {
            popEMail.IsOpen = false;
        }

        private void btnFermerloginfailed_Click(object sender, RoutedEventArgs e)
        {
            popLoginFailed.IsOpen = false;
        }
    }
}